﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SiteServer.Plugin;
using Thingpower.SS.EduOnline.Model;

namespace Thingpower.SS.EduOnline.Provider
{
    public class StudyLogDAO : BaseDAO
    {
        public const string TableName = "courseware_StudyLog";

        public static List<TableColumn> Columns => new List<TableColumn>
        {
            new TableColumn
            {
                AttributeName = nameof(EnrollmentInfo.Id),
                DataType = DataType.Integer
            },
            new TableColumn
            {
                AttributeName = nameof(EnrollmentInfo.CoursewareId),
                DataType = DataType.Integer
            },
            new TableColumn
            {
                AttributeName = nameof(EnrollmentInfo.UserId),
                DataType = DataType.Integer
            },
            new TableColumn
            {
                AttributeName = nameof(EnrollmentInfo.AddDate),
                DataType = DataType.DateTime

            }
        };

        public static int Insert(StudyLogInfo model)
        {
            string sqlString = $@"INSERT INTO {TableName}(
                {nameof(StudyLogInfo.CoursewareId)},
                {nameof(StudyLogInfo.UserId)},
                {nameof(StudyLogInfo.AddDate)}
            ) VALUES(
                @{nameof(StudyLogInfo.CoursewareId)},
                @{nameof(StudyLogInfo.UserId)},
                @{nameof(StudyLogInfo.AddDate)}
            )";

            var parameters = new[]
            {
                DataApi.GetParameter($"@{nameof(StudyLogInfo.CoursewareId)}", model.CoursewareId),
                DataApi.GetParameter($"@{nameof(StudyLogInfo.UserId)}", model.UserId),
                DataApi.GetParameter($"@{nameof(StudyLogInfo.AddDate)}", model.AddDate)
            };

            return DataApi.ExecuteNonQueryAndReturnId(TableName, nameof(StudyLogInfo.Id), ConnectionString, sqlString, parameters);
        }

        public static int GetCount(string queryExpression)
        {
            string sqlString =
                $"SELECT COUNT(*) FROM {TableName} WHERE {queryExpression}";

            return int.Parse(DataApi.ExecuteScalar(ConnectionString, sqlString).ToString());

        }

    }
}